'''
教师视图
'''
from interface import common_interface
from interface import teacher_interface
from lib import common

user_info = {
    'user': None
}


def login():
    while True:
        username = input('请输入用户名：').strip()
        password = input('请输入密码：').strip()

        flag, msg = common_interface.login_interface(
            username, password, user_type='teacher'
        )
        print(msg)
        if flag:
            # 可变类型不用global
            user_info['user'] = username
            break


@common.auth('teacher')
def list_course():
    course_list = teacher_interface.list_course(
        user_info.get('user')
    )
    if not course_list:
        print("未选择课程！")
    else:
        print(course_list)


@common.auth('teacher')
def select_course():
    while True:
        # 选择学校
        flag, school_list = common_interface.get_all_school_interface()
        if not flag:
            print("学校不存在")
            break

        for index, school_name in enumerate(school_list):
            print(index, school_name)

        choice = input("请输入学校编号：").strip()

        if not choice.isdigit():
            print('请输入数字！')
            continue

        choice = int(choice)
        if choice not in range(len(school_list)):
            print("请输入正确编号！")
            continue

        school_name = school_list[choice]

        # 选择课程
        course_list = common_interface.get_course_list_in_school(
            school_name
        )

        if not course_list:
            print('该学校课程为空，请联系管理员！')
            break

        for index, name in enumerate(course_list):
            print(index, name)

        choice2 = input("请输入课程编号：").strip()

        if not choice2.isdigit():
            print('请输入数字！')
            continue

        choice2 = int(choice2)
        if choice2 not in range(len(course_list)):
            print("请输入正确编号！")
            continue

        course_name = course_list[choice2]

        flag, msg = teacher_interface.select_course_interface(
            course_name, user_info.get('user')
        )
        print(msg)
        if flag:
            break


@common.auth('teacher')
def list_student():
    while True:
        course_list = teacher_interface.list_course(
            user_info.get('user')
        )
        if not course_list:
            print("未选择课程！")
            break

        for index, name in enumerate(course_list):
            print(index, name)

        choice = input("请输入课程编号：").strip()

        if not choice.isdigit():
            print('请输入数字！')
            continue

        choice = int(choice)
        if choice not in range(len(course_list)):
            print("请输入正确编号！")
            continue

        course_name = course_list[choice]

        student_list = teacher_interface.list_student_interface(
            course_name, user_info.get('user')
        )
        if student_list:
            print(student_list)
            break
        else:
            print('该课程学生列表为空')
            break


@common.auth('teacher')
def manage_student():
    while True:
        course_list = teacher_interface.list_course(
            user_info.get('user')
        )
        if not course_list:
            print("未选择课程！")
            break

        for index, name in enumerate(course_list):
            print(index, name)

        choice = input("请输入课程编号：").strip()

        if not choice.isdigit():
            print('请输入数字！')
            continue

        choice = int(choice)
        if choice not in range(len(course_list)):
            print("请输入正确编号！")
            continue

        course_name = course_list[choice]

        student_list = teacher_interface.list_student_interface(
            course_name, user_info.get('user')
        )
        if not student_list:
            print('该课程学生列表为空')
            break

        for index, name in enumerate(student_list):
            print(index, name)

        choice2 = input("请输入学生编号：").strip()

        if not choice2.isdigit():
            print('请输入数字！')
            continue

        choice2 = int(choice2)
        if choice2 not in range(len(student_list)):
            print("请输入正确编号！")
            continue

        student_name = student_list[choice2]

        score=input('请输入你要修改的分数:').strip()
        if not score.isdigit():
            print('请输入数字！')
            continue

        score = int(score)
        if score not in range(101):
            print("请输入0-100正确分数！")
            continue

        teacher_interface.manage_student_interface(
            student_name,course_name,score,user_info.get('user')
        )
        print('修改成功！')



teacher_dic = {
    '1': login,
    '2': list_course,
    '3': select_course,
    '4': list_student,
    '5': manage_student
}


def view():
    while True:
        print('''
        ====== 欢迎来到选课系统 ======
                1.登录
                2.查看课程
                3.选择课程
                4.查看学生
                5.管理分数
        =========== end ===========
        ''')

        choice = input('请输入功能编号：').strip()
        if choice not in teacher_dic:
            print("请输入正确的编号！")
            continue

        teacher_dic.get(choice)()
